package com.lixin.aopdemo2;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class MyLog {

    @Before("execution(* com.lixin.aopdemo2.GoodsServiceImpl.*(..))")
    public void before() {
        System.out.println("操作商品前的日志");
    }

    @After("execution(* com.lixin.aopdemo2.GoodsServiceImpl.*(..))")
    public void after() {
        System.out.println("操作商品后的日志");
    }

    @Around("execution(* com.lixin.aopdemo2.GoodsServiceImpl.*(..))")
    public void around(ProceedingJoinPoint pj) {
        try {
            System.out.println("环绕前"); // 这个比before要先执行
            Signature signature = pj.getSignature();
            System.out.println("签名:" + signature);
            // 执行目标方法
            Object proceed = pj.proceed();
            System.out.println("方法执行的结果：" + proceed);
            System.out.println("环绕后"); // 这个要比after后执行
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

}
